def get_bigger(num):
    if num < 0:
        raise Exception('非法输入')
    c1, c0, tmp = 0, 0, num
    while tmp & 1 == 0:
        c0 += 1
        tmp >>= 1
    while tmp & 1 == 1:
        c1 += 1
        tmp >>= 1
    # 出现形如……111000……或……000111……
    if c1 + c0 == 31:
        raise Exception('非法输入')
    p = c1 + c0
    num |= 1 << p
    num &= -1 << p
    num |= ~(-1 << (c1 - 1))
    return num


def get_smaller(num):
    if num < 0:
        raise Exception('非法输入')
    c1, c0, tmp = 0, 0, num
    while tmp & 1 == 1:
        c1 += 1
        tmp >>= 1
    while tmp & 1 == 0:
        c0 += 1
        tmp >>= 1
    # 出现形如……111000……或……000111……
    if c1 + c0 == 31:
        raise Exception('非法输入')
    p = c1 + c0
    num &= ~(1 << p)
    num &= -1 << p
    num |= (~(-1 << (c1 + 1))) << (c0 - 1)
    return num


print(get_bigger(10))
print(get_smaller(10))
